package calao.client.ui;

import calao.client.Desktop;
import calao.client.Images;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Widget;

public abstract class DataGrid
extends Composite
{

	final static Images images = Desktop.getDesktop().images;

	final static String STYLE_NAME = "calao-DataGrid";

	protected final Grid grid;
	private final Format[] columnFormat;

	protected DataGrid(int columns)
	{
		grid = new Grid(1, columns);
		grid.addStyleName(STYLE_NAME);
		grid.getRowFormatter().addStyleName(0, Styles.HEADER);
		columnFormat = new Format[columns];
	}
	
	abstract protected int getRowCount();

	abstract protected Object getData(int row, int col);
	
	protected void onResize() { }

	protected Object getViewData(int row, int col)
	{
		return getData(row, col);
	}
	
	public void refresh()
	{
		resizeRows(getRowCount());
		for (int row=0; row<getRowCount(); row++) refresh(row);
	}
	
	public void refresh(int row)
	{
		for (int col=0; col<grid.getColumnCount(); col++) {
			refresh(row, col);
		}		
	}

	public void refresh(int row, int col)
	{
		Format format = columnFormat[col];
		if (format==null) format = Format.STRING;
		Object data = getViewData(row, col);
		if (data==null) setText(row, col, "");
		else {
			String text = format.formatAsText(data);
			if (text!=null) setText(row, col, text);
			else setWidget(row, col, format.formatAsWidget(data));
		}
	}

	public void setColumnFormat(int col, Format format)
	{
		columnFormat[col] = format;
	}
	
	private void setCellFormat(int row, int col, Format format) {
		if (format!=null && format.alignsRight()) {
			addCellStyle(row, col, Styles.ALIGN_RIGHT);
		}
		else removeCellStyle(row, col, Styles.ALIGN_RIGHT);		
	}

	public void setHeader(int col, String text)
	{
		grid.setText(0, col, text);
	}

	public void setHeader(int col, Widget w)
	{
		grid.setWidget(0, col, w);
	}
	
	public Widget getHeaderWidget(int col)
	{
		return grid.getWidget(0, col);
	}
	
	private void resizeRows(int numRows)
	{
		int oldRowCount = grid.getRowCount()-1;
		if (oldRowCount==numRows) return;
		grid.resizeRows(numRows+1);
		for (int row=oldRowCount; row<numRows; row++) {
			addRowStyle(row, row%2==0? Styles.EVEN : Styles.ODD);
			for (int col=0; col<getColumnCount(); col++) {
				setCellFormat(row, col, columnFormat[col]);
			}
		}
		onResize();
	}

	protected void setText(int row, int col, String text)
	{
		grid.setText(row+1, col, text);
	}

	protected void setWidget(int row, int col, Widget widget)
	{
		grid.setWidget(row+1, col, widget);
	}
	
	protected Widget getWidget(int row, int col)
	{
		return grid.getWidget(row+1, col);
	}
	
	protected String getText(int row, int col)
	{
		return grid.getText(row+1, col);
	}
	
	public int getColumnCount()
	{
		return grid.getColumnCount();
	}
	
	public void setColumnWidth(int col, String width)
	{
		grid.getColumnFormatter().setWidth(col, width);		
	}
	
	protected void addRowStyle(int row, String style)
	{
		grid.getRowFormatter().addStyleName(row+1, style);
	}

	protected void removeRowStyle(int row, String style)
	{
		grid.getRowFormatter().removeStyleName(row+1, style);
	}

	protected void addCellStyle(int row, int col, String style)
	{
		grid.getCellFormatter().addStyleName(row+1, col, style);
	}

	protected void removeCellStyle(int row, int col, String style)
	{
		grid.getCellFormatter().removeStyleName(row+1, col, style);
	}
	
	protected int getPhysicalRowCount()
	{
		return grid.getRowCount()-1;
	}

}
